#ifndef D3D_IMAGE_MODEL_H
#define D3D_IMAGE_MODEL_H 1

//recover 3d position, normal from images (depth map, normal map)
#include "cvxImage.h"
#include "cvxD3d9.h"
#include "nvmath.h"
#include <string>
#include <iostream>
using nv::vec3f;
using namespace std;

class D3dImageModel;

//input configuration of scene
class SceneConfig
{

public:
	string m_leftName;
	string m_rightName;
	string m_normalName;
	string m_depthName;
	string m_alphaName;
	float m_depthOffset;
	float m_depthResize;
	float m_focalLength;
	float m_sensorWidth;

public:
	SceneConfig()
	{
		m_depthOffset = -1.0f;
		m_depthResize = -1.0f;
	}

	friend ostream & operator<<(ostream & os, const SceneConfig &sc)
	{
		assert(os.good());
		os<<"left_name "<<sc.m_leftName<<endl;
		os<<"right_name "<<sc.m_rightName<<endl;
		os<<"normal_map "<<sc.m_normalName<<endl;
		os<<"depth_map "<<sc.m_depthName<<endl;
		os<<"alpha_map "<<sc.m_alphaName<<endl;
		os<<"depth_info "<<sc.m_depthOffset<<" "<<sc.m_depthResize<<" "<<"#offset_and_resize"<<endl;
		os<<"camera_info "<<sc.m_focalLength<<" "<<sc.m_sensorWidth<<" "<<"#focal_length_and_sensorWidth"<<endl;
		assert(os.good());
		return os;
	}
	friend istream & operator>>(istream & is, SceneConfig &sc)
	{
		string dump;
		is>>dump>>sc.m_leftName;
		is>>dump>>sc.m_rightName;
		is>>dump>>sc.m_normalName;
		is>>dump>>sc.m_depthName;
		is>>dump>>sc.m_alphaName;
		is>>dump>>sc.m_depthOffset>>sc.m_depthResize>>dump;
		is>>dump>>sc.m_focalLength>>sc.m_sensorWidth>>dump;
		assert(is.good());		
		return is;
	}
};

/*
left_image E:\\code\\d3d_stereo_image\\StereoCameraD3d\\StereoCameraD3d\\image_circle\\mid.png
right_image E:\\code\\d3d_stereo_image\\StereoCameraD3d\\StereoCameraD3d\\image_circle\\mid.png
normal_map E:\\code\\d3d_stereo_image\\StereoCameraD3d\\StereoCameraD3d\\image_circle\\normal.png
depth_map E:\\code\\d3d_stereo_image\\StereoCameraD3d\\StereoCameraD3d\\image_circle\\depth.png
alpha_map E:\\code\\d3d_stereo_image\\StereoCameraD3d\\StereoCameraD3d\\image_circle\\alpha.png
depth_info -4.0 0.5 #offset and resize
*/



class D3dImageModel
{
	Mat m_mid;        //middle image
	Mat m_depth;
	Mat m_normal;
	Mat m_alpha;
	
	SceneConfig m_scene;

	vec3f m_vNormal;
	vec3f m_vPosition;

public:
	D3dImageModel();
	~D3dImageModel();

	vec3f normal(){return m_vNormal;}
	vec3f position(){return m_vPosition;}

	//load configuration from file
	void loafConfig(const string &fileName);
	void load(string imageName, string depthMapName, string normalMapName, string alphaMapName);

	//calculate normal and 3D position
	void calcNormal(int x, int y, float &nx, float &ny, float &nz);
	void calcPosition(int x, int y, float &px, float &py, float &pz);

};

#endif